package main.java.indi.zyj.lcweek;

import java.util.*;

public class MinOperations {

    public int minOperations(int[] nums) {

        int len = nums.length;
        Set<Integer> hashset = new HashSet<>();
        for (int num : nums) {
            hashset.add(num);
        }

        List<Integer> list = new ArrayList<>(hashset);
        Collections.sort(list);

        int j = 0, res = len;
        for (int i = 0; i < list.size(); i++) {
            int left = list.get(i);
            int right = left + len - 1;
            while (j < list.size() && list.get(j) <= right) {
                res = Math.min(res, len - (j - i + 1));
                j++;
            }
        }
        return res;

    }



}
